/*
 * @lc app=leetcode.cn id=653 lang=javascript
 *
 * [653] 两数之和 IV - 输入 BST
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {boolean}
 */
var findTarget = function(root, k) {
  let arr = [];
  function inorder(root) {
    if(!root) return;
    inorder(root.left);
    arr.push(root.val)
    inorder(root.right);
  }
  inorder(root);

  let l = 0, r = arr.length - 1;
  while(l < r) {
    if(arr[l] + arr[r] === k) return true;
    if(arr[l] + arr[r] < k) l++;
    if(arr[l] + arr[r] > k) r--;
  }
  return false;
};
// @lc code=end

